package com.ymatou.iapi.app.keyword.testcase;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.app.keyword.parameter.JyhBean;
import com.ymatou.iapi.app.keyword.service.JyhCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.database.sqlwapper.CSearchWapper;
import com.ymttest.database.sqlwapper.SearchWapper;
/**
*视频直播搜索接口
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_Jyh{
	private static JyhBean jyhBean;
	private static JyhCall jyhCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("通过品牌、类目标签搜索商品接口");
	}

	@Before
	public void caseUp() {
		jyhBean=new JyhBean();
		jyhCall=new JyhCall();
		//固定参数传值
		jyhBean.setAppkey(3);
		jyhBean.setExplain("False");
		jyhBean.setModule("pdt_list_md");
		jyhBean.setPlatform("appbuyer");
		jyhBean.setRequestid("22624344952400001322470125961");
		
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(jyhCall.getOpurl());
		
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Jyh_001() {
		Logger.start(true,"根据品牌搜索商品，检查返回数据是否全部为该搜索品牌数据 ");
		try {
			//设置参数
			int ibrandid = 10134;
			
			jyhBean.setFilter("term(ibrandid,"+ibrandid+")");
			jyhBean.setFl("sproductid,source,sproduct,ibrandid,ismerchant");
			
			jyhCall.setData(jyhBean);
			jyhCall.callService();			
			
			Logger.verifyEquals("OK", jyhCall.getStatus(), "验证接口返回状态是否正常");
			
			for (int i = 1; i <= jyhCall.getHits().length(); i++) {

				String info = jyhCall.getHits().get(i - 1).toString();
				JSONObject infoAry = new JSONObject(info);

				int brandId = (Integer) infoAry.get("ibrandid");
				Logger.verifyEquals(ibrandid, brandId, "验证第 "+i+"条数据的品牌id是否为条件中的品牌id值");
				
			}

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Jyh_002() {
		Logger.start(true,"根据品牌搜索商品，检查接口返回商品在数据库中是否对应该品牌 ");
		try {
			//设置参数
			int ibrandid = 10134;
			
			jyhBean.setFilter("term(ibrandid,"+ibrandid+")");
			jyhBean.setFl("sproductid,source,sproduct,ibrandid");
			
			jyhCall.setData(jyhBean);
			jyhCall.callService();			
			
			Logger.verifyEquals("OK", jyhCall.getStatus(), "验证接口返回状态是否正常");
			
			for (int i = 1; i <= jyhCall.getHits().length(); i++) {

				String info = jyhCall.getHits().get(i - 1).toString();
				JSONObject infoAry = new JSONObject(info);
				
				//如果接口返回的source=1为商户查M端库。source=0为买手，查C端的库
				int source = (Integer) infoAry.get("source");
				String productId = (String) infoAry.get("sproductid");
				
				List<Map> ProductLists = null;
				if (source==1) {
					//返回M端数据库里的商品信息
					 ProductLists = new SearchWapper().selectProductInfo(productId);
				}else{
					//返回C端数据库里的商品信息
					 ProductLists = new CSearchWapper().selectProductInfo(productId);
				}
				
				if(ProductLists.size()<0){
					Logger.verifyEquals(ProductLists.size()>0, ProductLists.size()<0, "检查第"+i+"条商品id为："+productId+"的商品在数据库中没有找到");
				}else{
					Logger.verifyEquals(ibrandid,ProductLists.get(0).get("iBrandId") , "检查第"+i+"条商品id为："+productId+"的商品品牌与数据库中一致");
				}
				
			}

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Jyh_003() {
		Logger.start(true,"根据品牌搜索商品，检查接口返回商品在数据库中是否在有效期内，状态是否为上架状态 ");
		try {
			//设置参数
			int ibrandid = 10134;
			
			jyhBean.setFilter("term(ibrandid,"+ibrandid+")");
			jyhBean.setFl("sproductid,source,sproduct,ibrandid");
			
			jyhCall.setData(jyhBean);
			jyhCall.callService();			
			
			Logger.verifyEquals("OK", jyhCall.getStatus(), "验证接口返回状态是否正常");
			
			for (int i = 1; i <= jyhCall.getHits().length(); i++) {

				String info = jyhCall.getHits().get(i - 1).toString();
				JSONObject infoAry = new JSONObject(info);
				
				//如果接口返回的source=1为商户查M端库。source=0为买手，查C端的库
				int source = (Integer) infoAry.get("source");
				String productId = (String) infoAry.get("sproductid");
				
				
				List<Map> ProductLists = null;
				if (source==1) {
					//返回M端数据库里的商品信息
					 ProductLists = new SearchWapper().selectProductInfo(productId);
				}else{
					//返回C端数据库里的商品信息
					 ProductLists = new CSearchWapper().selectProductInfo(productId);
				}
				
				if(ProductLists.size()<0){
					Logger.verifyEquals(ProductLists.size()>0, ProductLists.size()<0, "检查第"+i+"条商品id为："+productId+"的商品在数据库中没有找到");
				}else{
					
					int action = (Integer) ProductLists.get(0).get("iaction");
					Date validend =  (Date) ProductLists.get(0).get("validend");
					long nowTime = new Date().getTime();
					long endTime = validend.getTime();
					Logger.verifyEquals(true, action>-1, "检查第"+i+"条商品id为："+productId+"的商品在数据库中状态为有效");
					Logger.verifyEquals(true,endTime>nowTime , "检查第"+i+"条商品id为："+productId+"的商品在数据库中还未过期");
				}
				
			}

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Jyh_004() {
		Logger.start(true,"根据品牌搜索商品，检查接口返回商品在数据库中库存 数要大于0");
		try {
			//设置参数
			int ibrandid = 10134;
			
			jyhBean.setFilter("term(ibrandid,"+ibrandid+")");
			jyhBean.setFl("sproductid,source,sproduct,ibrandid");
			
			jyhCall.setData(jyhBean);
			jyhCall.callService();			
			
			Logger.verifyEquals("OK", jyhCall.getStatus(), "验证接口返回状态是否正常");
			
			for (int i = 1; i <= jyhCall.getHits().length(); i++) {

				String info = jyhCall.getHits().get(i - 1).toString();
				JSONObject infoAry = new JSONObject(info);
				
				//如果接口返回的source=1为商户查M端库。source=0为买手，查C端的库
				int source = (Integer) infoAry.get("source");
				String productId = (String) infoAry.get("sproductid");
				
				
				List<Map> ProductLists = null;
				if (source==1) {
					//返回M端数据库里的商品信息
					ProductLists = new SearchWapper().selectProductInfo(productId);
				}else{
					//返回C端数据库里的商品信息
					 ProductLists = new CSearchWapper().selectProductInfo(productId);
				}

				if(ProductLists.size()<0){
					Logger.verifyEquals(ProductLists.size()>0, ProductLists.size()<0, "检查第"+i+"条商品id为："+productId+"的商品在数据库中没有找到");
				}else{
					
					int stockNum = (Integer) ProductLists.get(0).get("itotalstock");
					
					Logger.verifyEquals(true, stockNum>0, "检查第"+i+"条商品id为："+productId+"的商品在数据库中库存数大于0");
					
				}
				
			}

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Jyh_005() {
		Logger.start(true,"根据品牌搜索商品，检查接口返回商品对比数据库是否属于砍价团商品");
		try {
			//设置参数
			int ibrandid = 10134;
			
			jyhBean.setFilter("term(ibrandid,"+ibrandid+")");
			jyhBean.setFl("sproductid,source,sproduct,ibrandid,iuserid");
			
			jyhCall.setData(jyhBean);
			jyhCall.callService();			
			
			Logger.verifyEquals("OK", jyhCall.getStatus(), "验证接口返回状态是否正常");
			
			for (int i = 1; i <= jyhCall.getHits().length(); i++) {

				String info = jyhCall.getHits().get(i - 1).toString();
				JSONObject infoAry = new JSONObject(info);
				
				//如果接口返回的source=1为商户查M端库。source=0为买手，查C端的库
				int source = (Integer) infoAry.get("source");
				String productId = (String) infoAry.get("sproductid");
				
				
				List<Map> HiddenProductLists = null;
				if (source==1) {
					//返回M端数据库里的商品信息，只有M端有砍价团商品
					HiddenProductLists = new SearchWapper().getHiddenProductInfo(productId);
				}
				Logger.verifyEquals(null, HiddenProductLists.size()==0 ? null:HiddenProductLists.size(), "验证第"+i+"条商品id为:"+productId+"的记录不存在隐藏商品表中");
				
			}

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Jyh_006() {
		Logger.start(true,"根据品牌搜索商品，检查接口返回商品对应的商户是否在白名单中");
		try {
			//设置参数
			int ibrandid = 10134;
			
			jyhBean.setFilter("term(ibrandid,"+ibrandid+")");
			jyhBean.setFl("sproductid,source,sproduct,ibrandid,iuserid");
			
			jyhCall.setData(jyhBean);
			jyhCall.callService();			
			
			Logger.verifyEquals("OK", jyhCall.getStatus(), "验证接口返回状态是否正常");
			
			for (int i = 1; i <= jyhCall.getHits().length(); i++) {

				String info = jyhCall.getHits().get(i - 1).toString();
				JSONObject infoAry = new JSONObject(info);
				
				//如果接口返回的source=1为商户查M端库。source=0为买手，查C端的库
				int source = (Integer) infoAry.get("source");
				int uid = (Integer) infoAry.get("iuserid");
				String productId = (String) infoAry.get("sproductid");
				
				boolean SellerAttribute = true;
				if (source==1) {
					//M端商品才需要验证是否在白名单
					SellerAttribute = new SearchWapper().getSellerAttributeInfo(uid);
				}
				
				Logger.verifyEquals(true,SellerAttribute , "检查第"+i+"条商品id为:"+productId+"条商品的商户是否在白名单中");
			}

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Jyh_007() {
		Logger.start(true,"根据品牌搜索商品，检查接口返回商品对应的商户物流方式是否满足被搜出条件");
		try {
			//设置参数
			int ibrandid = 10134;
			
			jyhBean.setFilter("term(ibrandid,"+ibrandid+")");
			jyhBean.setFl("sproductid,source,sproduct,ibrandid,iuserid");
			
			jyhCall.setData(jyhBean);
			jyhCall.callService();			
			
			Logger.verifyEquals("OK", jyhCall.getStatus(), "验证接口返回状态是否正常");
			
			for (int i = 1; i <= jyhCall.getHits().length(); i++) {

				String info = jyhCall.getHits().get(i - 1).toString();
				JSONObject infoAry = new JSONObject(info);
				
				//如果接口返回的source=1为商户查M端库。source=0为买手，查C端的库
				int source = (Integer) infoAry.get("source");
				int uid = (Integer) infoAry.get("iuserid");
				String productId = (String) infoAry.get("sproductid");
				
				List<Map> ProductLists = null;
				if (source==1) {
					ProductLists = new SearchWapper().selectProductInfo(productId);
					
				}else{
					ProductLists = new CSearchWapper().selectProductInfo(productId);
				}
				
				//获取商品运输方式，当isailprotected=1 or iCatalogStatus=4 or iCatalogStatus=5 都是满足要求的
				
				if((Integer) ProductLists.get(0).get("isailprotected")==1 || (Integer) ProductLists.get(0).get("iCatalogStatus")==4 || (Integer) ProductLists.get(0).get("iCatalogStatus")==5 ){
					Logger.verifyEquals(true, true, "检查第"+i+i+"条商品id为:"+productId+"条商品的物流方式是贝海直邮或者杭州保税或者卖家保税之一");
				}else{
					Logger.verifyEquals(true, false, "检查第"+i+"条商品id为:"+productId+"的物流方式是贝海直邮或者杭州保税或者卖家保税之一");
				}
			}

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Jyh_011() {
		Logger.start(true,"根据品牌搜索商品，检查分页功能是否正常 ");
		try {
			//设置参数
			int ibrandid = 10134;
			int onePage = 2; //设置每页显示2条数据
			jyhBean.setFilter("term(ibrandid,"+ibrandid+")");
			jyhBean.setFl("sproductid,source,sproduct,ibrandid,ismerchant");
			jyhBean.setLimit("0,"+onePage+"");
			
			jyhCall.setData(jyhBean);
			jyhCall.callService();			
			
			Logger.verifyEquals("OK", jyhCall.getStatus(), "验证接口返回状态是否正常");
			
		Logger.verifyEquals(onePage, jyhCall.getHits().length(), "查看每页是否显示"+onePage+"条记录");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
/*	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Jyh_012() {
		Logger.start(true,"根据类目搜索商品，检查返回数据是否全部为该搜索类目数据 ");
		try {
			//设置参数
			int categoryId =1178;
			
			//TODO 需要确认类目的参数名是否为fparentcategoryarrayids
			jyhBean.setFilter("term(fparentcategoryarrayids,"+categoryId+")");
			jyhBean.setFl("icategoryid,sproductid,source,sproduct,ibrandid,ismerchant");
			
			jyhCall.setData(jyhBean);
			jyhCall.callService();			
			
			Logger.verifyEquals("OK", jyhCall.getStatus(), "验证接口返回状态是否正常");
			

		} catch (Exception e) {
			Logger.fail(e);
		}
	}*/
	
	//按类目搜索的case还没有写
}